﻿using UnityEngine;

public class SpawnManagerX : MonoBehaviour
{
    public GameObject[] ballPrefabs;

    private float spawnLimitXLeft = -22;
    private float spawnLimitXRight = 7;
    private float spawnPosY = 30;

    //private float startDelay =1f;
    private float spawnInterval;

    // Start is called before the first frame update
    void Start()
    {
        //spawnInterval = Random.Range(3f,4f);不可行start（）方法只会执行一次
        //InvokeRepeating("SpawnRandomBall", startDelay, Random.Range(3f, 4f));//不可行，这么些没有意义
        NextTime();

    }

    // Spawn random ball at random x position at top of play area
    void SpawnRandomBall ()
    {
        int index = Random.Range(0,ballPrefabs.Length);
        // Generate random ball index and random spawn position
        Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);

        // instantiate ball at random spawn location
        Instantiate(ballPrefabs[index], spawnPos, ballPrefabs[index].transform.rotation);
    }
    void NextTime()
    {
        spawnInterval = Random.Range(2f,5f);//生成一个2—5的随机数
        Invoke("SpawnRandomBall",spawnInterval);
        Invoke("OnTimer",spawnInterval);
    }

    void OnTimer()
    {
       // Debug.Log(Time.time);
        NextTime();
    }

}
